#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

class Context
{
public:
    Context(int num)
    {
        m_num = num;
    }

    void setNum(int num)
    {
        m_num = num;
    }
    int getNum()
    {
        return m_num;
    }
    void setRes(int res)
    {
        m_res = res;
    }
    int getRes()
    {
        return m_res;
    }

private:
    int m_num, m_res;
};

class Expression
{
public:
    // ~Expression()        // 不能触发 派生类的析构
    virtual ~Expression()   // 派生类主动调用 析构函数
    {
        std::cout << "~Expression 析构" << std::endl;
    };
    virtual void interpreter(Context *context) = 0;
};

class AddExpression : public Expression
{
public:
    ~AddExpression()
    {
        std::cout << "AddExpression 析构" << std::endl;
    }
    void interpreter(Context *context)
    {
        int num = context->getNum();
        num++;
        context->setNum(num);
        context->setRes(num);
        std::cout << "~PlusExpression：" << context->getRes() << std::endl;
    }
};

class DelExpression : public Expression
{
public:
    ~DelExpression()
    {
        std::cout << "~DelExpression 析构" << std::endl;
    }
    void interpreter(Context *context)
    {
        int num = context->getNum();
        num--;
        context->setNum(num);
        context->setRes(num);
        std::cout << "MinusExpression：" << context->getRes() << std::endl;
    }
};

int main()
{
    Context *pcxt = new Context(10);
    Expression *e1 = new AddExpression();
    Expression *e2 = new DelExpression();

    e1->interpreter(pcxt);
    e2->interpreter(pcxt);

    delete e1;
    delete e2;
    delete pcxt;

    return 0;
}
